<?php require 'inc_security.php'; 

//Kiem tra quyen addedit
checkPermission('add');
$whs_supplies_id = getValue("whs_supplies_id","int","POST",0);
$whs_warehouse_id = getValue("whs_warehouse_id","int","POST",0);
$whs_supplies_price = getValue("whs_supplies_price","int","POST",0);
$whs_supplies_number = getValue("whs_supplies_number","int","POST",0);
$whs_supplies_price_total = $whs_supplies_price*$whs_supplies_number;
$whs_supplies_name = $list_supplies[$whs_supplies_id];
$whs_date = time();
$myform = new generate_form();
$myform->addTable($bg_table);
$myform->add('whs_supplies_id','whs_supplies_id',0,0,'',1,'Bạn chưa nhập tên vật tư');
$myform->add('whs_supplies_name','whs_supplies_name',0,1,'',1,'Bạn chưa nhập tên vật tư');
$myform->add('whs_supplies_number','whs_supplies_number',1,0,0);
$myform->add('whs_supplies_price','whs_supplies_price',1,0,0);
$myform->add('whs_supplies_price_total','whs_supplies_price_total',1,1,0);
$myform->add('whs_warehouse_id','whs_warehouse_id',1,0,0);
$myform->add('whs_date','whs_date',1,1,0);
$action = getValue('action','str','POST','');
if($action == 'execute'){
	$bg_errorMsg .= $myform->checkData();
	if(!$bg_errorMsg){
		$db_ex = new db_execute_return();
		$last_id = $db_ex->db_execute($myform->generate_insert_SQL());
		unset($db_ex);
         $myform_report = new generate_form();
         $myform_report->addTable('warehouse_report');
      $db_report = db_first("SELECT * FROM warehouse_report WHERE whr_supplies_id =".$whs_supplies_id." AND whr_warehouse_id =".$whs_warehouse_id);
      if($db_report == NULL){
         $whr_supplies_id = $whs_supplies_id;
         $whr_warehouse_id = $whs_warehouse_id;
         $whr_supplies_number = $whs_supplies_number;
         $whr_supplies_price = $whs_supplies_price;
         $whr_supplies_name = $list_supplies[$whr_supplies_id];
         $myform_report->add('whr_supplies_id','whr_supplies_id',0,1,'',1,'Bạn chưa nhập tên vật tư');
         $myform_report->add('whr_supplies_name','whr_supplies_name',0,1,'',1,'Bạn chưa nhập tên vật tư');
         $myform_report->add('whr_supplies_number','whr_supplies_number',1,1,0);
         $myform_report->add('whr_warehouse_id','whr_warehouse_id',1,1,0);
         $myform_report->add('whr_supplies_price','whr_supplies_price',1,1,0);
         $db_ex = new db_execute_return();
   		$last_id = $db_ex->db_execute($myform_report->generate_insert_SQL());
   		unset($db_ex);
      }else{
         $whr_supplies_number = $db_report['whr_supplies_number']+$whs_supplies_number;
         $whr_supplies_price = $whs_supplies_price;
         $myform_report->add('whr_supplies_number','whr_supplies_number',1,1,0);
         $myform_report->add('whr_supplies_price','whr_supplies_price',1,1,0);
         $db_ex = new db_execute($myform_report->generate_update_SQL('whr_id', $db_report['whr_id']));
			unset($db_ex);
      }
		redirect('listing.php');
	}
}

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="vi" lang="vi" xmlns:og="http://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?=$load_header?>
<style>
	.list-role label {
		margin-bottom: 0;
		margin-top : 6px;
		margin-right : 20px;
	}
	.list-role input[type="checkbox"]{
		margin-top:-1px;
		margin-right:2px;
	}
</style>
</head>
<body>
<div class="module_header bold fix"><?=$module_name?></div>
<div id="wrapper">
    <?=$bg_errorMsg?>
	<?php $form = new form();
	?>
	<?=$form->form_open()?>
    <?=$form->textnote('Các trường có dấu (<span class="form-asterick">*</span>) là bắt buộc nhập')?>
    <?//=$form->text(array('label'=>'Mã vật từ','name'=>'whs_supplies_code','id'=>'whs_supplies_code','require'=>0,'errorMsg'=>'Bạn chưa nhập mã vật tư'))?>
	<?=$form->select(array('label'=>'Vật tư','name'=>'whs_supplies_id', 'id'=>'whs_supplies_id','option'=>$list_supplies, 'title'=>'Chọn kho','require'=>1,'errorMsg'=>'Bạn chưa chọn tên vật tư','selected'=>getValue('whs_supplies_id','int','POST',0)))?>
	<?=$form->text(array('label'=>'Số lượng','name'=>'whs_supplies_number','id'=>'whs_supplies_number'))?>
	<?=$form->text(array('label'=>'Đơn giá','name'=>'whs_supplies_price','id'=>'whs_supplies_price'))?>
   <?
      if($isAdmin || $warehouse_id == 0) echo $form->select(array('label'=>'Kho','name'=>'whs_warehouse_id', 'id'=>'whs_warehouse_id','option'=>$list_wah, 'title'=>'Chọn kho','require'=>1,'errorMsg'=>'Bạn chưa chọn kho','selected'=>getValue('whs_warehouse_id','int','POST',0)));
      else {
   ?>
         <input type="hidden" value="<?=$warehouse_id?>" name="whs_warehouse_id"/>
   <?
      }
   ?>
    <?=$form->form_action(array('label'=>array('Thêm mới','Nhập lại'),'type'=>array('submit','reset')))?>
	<?=$form->form_close()?>
</div>
</body>
</html>